package org.mespilus.mespilusweb.ui.usermanagement.client.presenter;

import java.util.ArrayList;
import java.util.List;

import org.mespilus.mespilusweb.ui.usermanagement.client.UserDetailsPanel;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.AddMemberEvent;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.MemberFoundEvent;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.MemberFoundHandler;
import org.mespilus.mespilusweb.ui.usermanagement.client.events.UserManagementBus;
import org.mespilus.mespilusweb.ui.usermanagement.client.model.FullMember;

public class UserDetailsPresenter implements MemberFoundHandler{
	protected UserDetailsPanel userDetailsPanel;
	
	public UserDetailsPresenter() {
		UserManagementBus.getInstance().registerToMemberFound(this);
	}
	
	public UserDetailsPanel getUserDetailsPanel() {
		return userDetailsPanel;
	}

	public void setUserDetailsPanel(UserDetailsPanel userDetailsPanel) {
		this.userDetailsPanel = userDetailsPanel;
	}

	@Override
	public void processMemberFound(MemberFoundEvent event) {
		FullMember person = event.getFullMember();
		List<String> data = new ArrayList<String>();
		data.add(person.getMail());
		data.add(person.getName());
		data.add(person.getFirstName());
		data.add(person.getSecondName());
		data.add(person.getNIF());
		data.add(person.getStreet());
		data.add(person.getPostalCode());
		data.add(person.getCity());
		data.add(person.getCountry());
		data.add(person.getPhone1());
		data.add(person.getPhone2());
		userDetailsPanel.fillValues(data);
	}

	public void saveUser(List<String> data) {
		FullMember person = new FullMember();
		person.setMail(data.get(0));
		person.setName(data.get(1));
		person.setFirstName(data.get(2));
		person.setSecondName(data.get(3));
		person.setNIF(data.get(4));
		person.setStreet(data.get(5));
		person.setPostalCode(data.get(6));
		person.setCity(data.get(7));
		person.setCountry(data.get(8));
		person.setPhone1(data.get(9));
		person.setPhone2(data.get(10));
		AddMemberEvent event = new AddMemberEvent();
		event.setMember(person);
		UserManagementBus.getInstance().fireUserAddEvent(event);
	}
}
